package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.server.pojo.User;
import com.tanhua.server.utils.UserThreadLocal;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/2/25 18:29
 * @version: 0.0.1
 * @modified By:
 */
@Service
public class QuanziMQService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    private static final Logger LOGGER=LoggerFactory.getLogger(QuanziMQService.class);
    /**
     * @description 发布动态消息
     * @author
     * @date
     * @param
     *@return
     */
    public void publishMsg(String publishId){
        this.sendMsg(1,publishId);
    }
    /**
     * @description 查询动态消息
     * @author
     * @date
     * @param
     *@return
     */
    public void queryPublishMsg(String publishId){
        this.sendMsg(2,publishId);
    }
    /**
     * @description 点赞动态消息
     * @author
     * @date
     * @param
     *@return
     */
    public void likePublishMsg(String publishId){
        this.sendMsg(3,publishId);
    }
    /**
     * @description 喜欢动态消息
     * @author
     * @date
     * @param
     *@return
     */
    public void lovePublishMsg(String publishId){
        this.sendMsg(4,publishId);
    }
    /**
     * @description 评论动态消息
     * @author
     * @date
     * @param
     *@return
     */
    public void commentPublishMsg(String publishId){
        this.sendMsg(5,publishId);
    }
    /**
     * @description 取消点赞动态消息
     * @author
     * @date
     * @param
     *@return
     */
    public void disLikePublishMsg(String publishId){
        this.sendMsg(6,publishId);
    }
    /**
     * @description 取消喜欢动态消息
     * @author
     * @date
     * @param
     *@return
     */
    public void disLovePublishMsg(String publishId){
        this.sendMsg(7,publishId);
    }
    /**
     * @description 发送圈子操作相关的消息
     * @author
     * @date
     * @param
     *@return
     */
    private void sendMsg(Integer type,String publishId){
        try {
            User user= UserThreadLocal.get();
            Publish publish = this.quanZiApi.queryPublishById(publishId);
            //构建消息
            Map<String,Object>msg=new HashMap<>();
            msg.put("type",type);
            msg.put("publishId",publishId);
            msg.put("userId",user.getId());
            msg.put("date",System.currentTimeMillis());
            msg.put("pid",publish.getPid());
            this.rocketMQTemplate.convertAndSend("tanhua-quanzi",msg);
        } catch (Exception e) {
            LOGGER.error("发送消息失败！type=" + type + ",publishId=" + publishId, e);
        }
    }
}
